﻿#region Using Statements
using System.Collections.Generic;
using System.Diagnostics;
using GameBase.Sounds;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
#endregion

namespace StarFox.Sounds
{
    /// <summary>
    /// Provides functionality for gameplay sound management.
    /// </summary>
    public class GamePlaySound : SoundManager
    {
        #region Fields

        Cue starship_motor;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="contentPath">Path of XACT audio related content.</param>
        /// <param name="xactProjectName">XACT project file name - .xap</param>
        /// <param name="waveBankName">Wave bank name - .xwb</param>
        /// <param name="soundBankName">Sound bank name - .xsb</param>
        public GamePlaySound(string contentPath, string xactProjectName, string waveBankName, string soundBankName)
            : base(contentPath, xactProjectName, waveBankName, soundBankName)
        {
        }

        #endregion

        #region Dispose

        // dispose flag
        bool isDisposed = false;

        /// <summary>
        /// Releases the unmanaged resources used by this object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (!isDisposed)
            {
                if (disposing)
                {
                    if (starship_motor != null) { starship_motor.Dispose(); starship_motor = null; }

                    base.Dispose(disposing);
                }

                isDisposed = true;
            }
        }

        #endregion

        #region Sound Configuration

        /// <summary>
        /// Sets the volume of all associated sounds.
        /// </summary>
        /// <param name="volume">Volume (0.0 -> silence - 1.0 -> full volume).</param>
        public void SetVolume(float volume)
        {
            SetGlobalVariable("Volume", volume);
        }

        #endregion

        #region PlayBack Methods

        /// <summary>
        /// Plays sound corresponding to "pass-by whosh" effect.
        /// </summary>
        /// <param name="asteroidPos">The asteroid position.</param>
        /// <param name="shipPos">The starship position.</param>
        public void Whosh(Vector3 asteroidPos, Vector3 shipPos)
        {
            PlayCue3D("WHOSH", shipPos, asteroidPos);
        }

        /// <summary>
        /// Plays sound corresponding to "got power-up" occurrence.
        /// </summary>
        public void PowerUp()
        {
            PlayCue("POWER_UP_MISC");
        }

        /// <summary>
        /// Plays sound corresponding to "got extra bomb" occurrence.
        /// </summary>
        public void ExtraBomb()
        {
            PlayCue("POWER_UP_BOMB");
        }

        /// <summary>
        /// Plays sound corresponding to "got extra life" occurrence.
        /// </summary>
        public void ExtraLife()
        {
            PlayCue("POWER_UP_LIFE");
        }

        /// <summary>
        /// Plays sound corresponding to "countdown - count" occurrence.
        /// </summary>
        public void Countdown_Count()
        {
            PlayCue("COUNTDOWN_COUNT");
        }

        /// <summary>
        /// Plays sound corresponding to "countdown - go" occurrence.
        /// </summary>
        public void Countdown_Go()
        {
            PlayCue("COUNTDOWN_GO");
        }

        /// <summary>
        /// Plays sound corresponding to "danger - asteroid collision" occurrence.
        /// </summary>
        public void Danger_Collision()
        {
            PlayCue("DANGER_COLLISION");
        }

        /// <summary>
        /// Plays sound corresponding to "danger - low health" occurrence.
        /// </summary>
        /// <param name="pitch">Pitch (0.0 -> min pitch - 1.0 -> max pitch).</param>
        public void Danger_LowHealth(float pitch)
        {
            List<CueInstanceVariable> variables = new List<CueInstanceVariable>() { new CueInstanceVariable("Pitch", pitch) };
            PlayCue("DANGER_LOW_HEALTH", variables);
        }

        /// <summary>
        /// Plays sound corresponding to "danger - out of fuel" occurrence.
        /// </summary>
        public void Danger_OutFuel()
        {
            PlayCue("DANGER_OUT_FUEL");
        }

        /// <summary>
        /// Plays sound corresponding to "bomb mode - activation" occurrence.
        /// </summary>
        public void BombMode_Activation()
        {
            PlayCue("BOMB_MODE_ACTIVATION");
        }

        /// <summary>
        /// Plays sound corresponding to "bomb mode - collision" occurrence.
        /// </summary>
        public void BombMode_Collision()
        {
            PlayCue("BOMB_MODE_COLLISION");
        }

        /// <summary>
        /// Starts playback of sound corresponding to "starship motor".
        /// </summary>
        public void Motor_Play()
        {
            starship_motor = GetCue("STARSHIP_MOTOR");
            starship_motor.Play();
        }

        /// <summary>
        /// Stops playback of sound corresponding to "starship motor".
        /// </summary>
        public void Motor_Stop()
        {
            starship_motor.Stop(AudioStopOptions.Immediate);
            starship_motor.Dispose();
            starship_motor = null;
        }

        /// <summary>
        /// Configures pitch of sound corresponding to "starship motor".
        /// </summary>
        /// <param name="pitch">Pitch (0.0 -> min pitch - 1.0 -> max pitch).</param>
        public void Motor_Pitch(float pitch)
        {
            starship_motor.SetVariable("Pitch", pitch);
        }

        /// <summary>
        /// Starts playback of sound corresponding to "game over" occurrence..
        /// </summary>
        public void GameOver()
        {
            PlayCue("GAMEOVER");
        }

        /// <summary>
        /// Starts playback of sound corresponding to "victory" occurrence..
        /// </summary>
        public void Victory()
        {
            PlayCue("VICTORY");
        }

        #endregion

        #region PlayAllSounds, PauseAllSounds, StopAllSounds

        /// <summary>
        /// Resumes playback of all sounds.
        /// </summary>
        public override void PlayAllSounds()
        {
            if (starship_motor != null) { starship_motor.Resume(); }
            base.PlayAllSounds();
        }

        /// <summary>
        /// Pauses playback of all sounds.
        /// </summary>
        public override void PauseAllSounds()
        {
            if (starship_motor != null) { starship_motor.Pause(); }
            base.PauseAllSounds();
        }

        /// <summary>
        /// Stops playback of all sounds.
        /// </summary>
        public override void StopAllSounds()
        {
            if (starship_motor != null) { starship_motor.Stop(AudioStopOptions.Immediate); }
            base.StopAllSounds();
        }

        #endregion
    }
}
